package com.g0dkar.leet.core.model.loaders;

import javax.persistence.Column;

import com.g0dkar.leet.core.model.Property;
import com.g0dkar.leet.util.common.StringUtils;
import com.g0dkar.leet.util.reflection.ReflectionUtils;

public class PropertyLoader implements ModelLoaderInterface<Property, ReflectionUtils.Field> {

	public Property load(ReflectionUtils.Field field) {
		Property property = new Property();
		
		property.setName(field.name);
		property.setDisplayName(StringUtils.classnameAsPhrase(StringUtils.capitalize(field.name)));
		property.setType(field.get.getReturnType());
		property.setClassName(field.get.getReturnType().getClass().getName());
		property.setGetter(field.get);
		property.setSetter(field.set);
		
		if (field.field != null && field.field.isAnnotationPresent(Column.class)) {
			Column column = field.field.getAnnotation(Column.class);
			property.setNotNull(!column.nullable());
			property.setUnique(column.unique());
		}
		else {
			property.setNotNull(false);
			property.setUnique(false);
		}
		
		return property;
	}
	
}
